package com.zjitc.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjitc.reggie.common.CustomException;
import com.zjitc.reggie.entity.Category;
import com.zjitc.reggie.entity.Dish;
import com.zjitc.reggie.entity.Setmeal;
import com.zjitc.reggie.mapper.CategoryMapper;
import com.zjitc.reggie.service.CategoryService;
import com.zjitc.reggie.service.DishService;
import com.zjitc.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId, id);
        int dishCount = dishService.count(dishWrapper);
        if (dishCount > 0) {
            throw new CustomException("关联菜品,不能删除");
        }
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.eq(Setmeal::getCategoryId, id);
        int setMealCount = setmealService.count(setmealQueryWrapper);
        if (setMealCount > 0) {
            throw new CustomException("关联套餐,不能删除");
        }
        super.removeById(id);
    }
}
